package com.vcc.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.vcc.core.exception.NotFoundException;
import com.vcc.core.exception.ProgramException;
import com.vcc.data.entity.Channel;
import com.vcc.data.mapper.ChannelMapper;
import com.vcc.service.boss.request.channel.BossChannelCreateRequest;
import com.vcc.service.boss.request.channel.BossChannelUpdateRequest;
import com.vcc.service.boss.response.channel.BossChannelResponse;
import com.vcc.service.service.ChannelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.vcc.data.entity.table.ChannelTableDef.CHANNEL;

@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, Channel> implements ChannelService {
    @Override
    public Boolean existsByCode(String code) {
        return queryChain().eq(Channel::getCode, code).exists();
    }

    @Override
    public Boolean createChannel(BossChannelCreateRequest request) {
        if (existsByCode(request.getCode())) {
            throw new ProgramException("渠道编码已存在");
        }
        Channel channel = new Channel();
        channel.setCode(request.getCode());
        channel.setName(request.getName());
        channel.setStatus(request.getStatus());
        channel.setRemark(request.getRemark());
        channel.setSort(request.getSort());
        channel.setHasShareAccount(request.getHasShareAccount());
        channel.setVerifyOpenCardStatus(request.getVerifyOpenCardStatus());
        channel.setVerifyRechargeStatus(request.getVerifyRechargeStatus());
        channel.setVerifyWithdrawStatus(request.getVerifyWithdrawStatus());
        channel.setVerifyFreezeStatus(request.getVerifyFreezeStatus());
        channel.setVerifyCloseStatus(request.getVerifyCloseStatus());
        return save(channel);
    }

    @Override
    public Boolean updateChannel(BossChannelUpdateRequest request) {
        Channel channel = getByIdOpt(request.getId()).orElseThrow(() -> new NotFoundException("渠道不存在"));

        if (StringUtils.isNotBlank(request.getCode()) && !channel.getCode().equals(request.getCode()) && existsByCode(request.getCode())) {
            throw new ProgramException("渠道编码已存在");
        }
        UpdateChain<Channel> updatedChain = updateChain();
        if (StringUtils.isNotBlank(request.getCode()) && !channel.getCode().equals(request.getCode())) {
            updatedChain.set(Channel::getCode, request.getCode());
        }
        if (StringUtils.isNotBlank(request.getName()) && !channel.getName().equals(request.getName())) {
            updatedChain.set(Channel::getName, request.getName());
        }
        if (request.getStatus() != null && !channel.getStatus().equals(request.getStatus())) {
            updatedChain.set(Channel::getStatus, request.getStatus());
        }
        if (request.getRemark() != null && !channel.getRemark().equals(request.getRemark())) {
            updatedChain.set(Channel::getRemark, request.getRemark());
        }
        if (request.getSort() != null && !channel.getSort().equals(request.getSort())) {
            updatedChain.set(Channel::getSort, request.getSort());
        }
        if (request.getHasShareAccount() != null && !channel.getHasShareAccount().equals(request.getHasShareAccount())) {
            updatedChain.set(Channel::getHasShareAccount, request.getHasShareAccount());
        }
        if (request.getVerifyOpenCardStatus() != null && !channel.getVerifyOpenCardStatus().equals(request.getVerifyOpenCardStatus())) {
            updatedChain.set(Channel::getVerifyOpenCardStatus, request.getVerifyOpenCardStatus());
        }
        if (request.getVerifyRechargeStatus() != null && !channel.getVerifyRechargeStatus().equals(request.getVerifyRechargeStatus())) {
            updatedChain.set(Channel::getVerifyRechargeStatus, request.getVerifyRechargeStatus());
        }
        if (request.getVerifyWithdrawStatus() != null && !channel.getVerifyWithdrawStatus().equals(request.getVerifyWithdrawStatus())) {
            updatedChain.set(Channel::getVerifyWithdrawStatus, request.getVerifyWithdrawStatus());
        }
        if (request.getVerifyFreezeStatus() != null && !channel.getVerifyFreezeStatus().equals(request.getVerifyFreezeStatus())) {
            updatedChain.set(Channel::getVerifyFreezeStatus, request.getVerifyFreezeStatus());
        }
        if (request.getVerifyCloseStatus() != null && !channel.getVerifyCloseStatus().equals(request.getVerifyCloseStatus())) {
            updatedChain.set(Channel::getVerifyCloseStatus, request.getVerifyCloseStatus());
        }
        updatedChain.eq(Channel::getId, request.getId());
        return updatedChain.update();
    }

    @Override
    public Page<BossChannelResponse> paging(Integer pageNumber, Integer pageSize, String name, String code, Integer status) {
        QueryChain<Channel> queryChain = queryChain();
        if (StringUtils.isNotBlank(name)) {
            queryChain.like(Channel::getName, name);
        }
        if (StringUtils.isNotBlank(code)) {
            queryChain.eq(Channel::getCode, code);
        }
        if (status != null) {
            queryChain.eq(Channel::getStatus, status);
        }
        queryChain.orderBy(Channel::getSort, true);
        return queryChain.pageAs(Page.of(pageNumber == null ? 1 : pageNumber, pageSize == null ? 20 : pageSize), BossChannelResponse.class);
    }

    @Override
    public List<BossChannelResponse> listByChannel(Boolean hasShareAccount, String value) {
        QueryChain<Channel> queryChain = queryChain();
        queryChain.and(
                CHANNEL.CODE.like(value,StringUtils::isNotBlank).or(CHANNEL.NAME.like(value,StringUtils::isNotBlank))
        );
        queryChain.where(CHANNEL.HAS_SHARE_ACCOUNT.eq(hasShareAccount, hasShareAccount != null));
        queryChain.eq(Channel::getStatus, 0);
        queryChain.orderBy(Channel::getSort, true);
        return queryChain.listAs(BossChannelResponse.class);
    }

    @Override
    public Channel getByCode(String code) {
        return queryChain().eq(Channel::getCode, code).oneOpt().orElseThrow(() -> new NotFoundException("渠道不存在"));
    }
}
